// pages/control/index.js
import config from '../../config/index.js'
import wxRequest from '../../utils/wxRequest.js'
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: 1, //当前页
    numbers: 20, //一页显示多少条
    total:0, //共多少条数据
    data: [],
    headArr: ['日期', '站点名称', '渠道', '复制总数', '总金额', '单价','操作']
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.getList();
  },
  //删除单个报表
  reDel(e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    wx.showModal({
      content: '确认删除报表？',
      confirmText: '确定',
      cancelText: '取消',
      success: function (res) {
        if (res.cancel) {
          //点击取消,默认隐藏弹框
        } else {
          //点击确定
          var map = {
            'id': id,
          }
          wxRequest.getRequest('/toolkit/copy/re_del', map).then(res => {
            console.log(res)
            wx.showToast({
              title: res.message,
              icon: res.code == 0 ? 'success' : 'none'
            })
            if (res.code == 0) {
              that.getList();
            }
          }).catch(error => { })
        }
      }
    })
  },
  //删除单个报表
  reDelAll() {
    var that = this;
    wx.showModal({
      content: '确认清空所有报表？',
      confirmText: '确定',
      cancelText: '取消',
      success: function (res) {
        if (res.cancel) {
          //点击取消,默认隐藏弹框
        } else {
          //点击确定
          wxRequest.getRequest('/toolkit/copy/re_delall').then(res => {
            console.log(res)
            wx.showToast({
              title: res.message,
              icon: res.code == 0 ? 'success' : 'none'
            })
            if (res.code == 0) {
              that.getList();
            }
          }).catch(error => { })
        }
      }
    })
  },
  //获取统计表
  getList() {
    var map = {
      'page': this.data.page,
      'number': this.data.numbers,
    }
    var that = this;
    wxRequest.getRequest('/toolkit/copy/report', map).then(res => {
      console.log(res)
      that.setData({
        data: res.data.data,
        total: res.data.total
      })
    }).catch(error => { })
  },
  // 上拉加载帖子列表
  loadlist() {
    var map = {
      'page': this.data.page + 1, //下一页
      'number': this.data.numbers,
    }
    var newsBefore = this.data.data; //旧数据
    // console.log(map)
    wx.showToast({
      title: '加载中...',
      icon: 'loading',
      duration: 500
    })
    wxRequest.getRequest('/toolkit/copy/report', map).then(res => {
      if (res.data.data != '') {
        this.setData({
          page: map.page,
          total: res.data.total,
          data: newsBefore.concat(res.data.data),
        })
      } else {
        wx.showToast({
          title: '数据已加载完',
          duration: 1000
        })
      }

    }).catch(error => { })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var that = this;
    that.setData({
      page: 1,
    })
    that.getList();
    wx.showToast({
      title: '刷新成功',
      duration: 1000
    })
    wx.setBackgroundTextStyle({
      textStyle: 'dark', // 下拉背景字体、loading 图的样式为dark
    })
    // 隐藏导航栏加载框  
    wx.hideNavigationBarLoading();
    // 停止下拉动作  
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    that.loadlist();
  }
})